<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\Bkuser;

class BkuserController extends Controller
{

    //后台用户列表
    public function bkuser_list(){
        $userrole=DB::table('bkuser')->where('username',session("username"))->value('role');
        // dd($userrole);
    	$arr=DB::table('bkuser')->get();
    	return view('/admin/bkuser/bkuser_list',['arr'=>$arr,'userrole'=>$userrole]);
    }
    //添加页面
    public function bkuser_add(){
    	return view('/admin/bkuser/bkuser_add');
    }
    //添加至数据库
    public function bkuser_insert(Request $request){
    	// dd($request->all());
        // return $data;
    	if(DB::table('bkuser')->insert($request->except('confirm_password'))){
    		return 1;
    	}else{
    		return 0;
    	}
    	// return redirect()->action('admin\BkuserController@bkuser_list');
    }
    // 从数据库中删除
    public function bkuser_del(Request $request){
    	if(DB::table('bkuser')->whereIn('id',$request->ids)->delete()){
    		return 1;
    	}else{
    		return 0;
    	}
    }
    // 修改页面
    public function bkuser_edit(Request $request){
    	$piece=DB::table('bkuser')->find($request->id);
    	return view('/admin/bkuser/bkuser_edit',['piece'=>$piece,'nrow'=>$request->nrow]);
    }
    // 修改数据库内容
    public function bkuser_update(Request $request){
    	// return $request->state;
    	if($request->state){
    		$state=DB::table('bkuser')->where('id',$request->id)->value('state');
 		   	// return $request->all();
    		// return $data;
			if($state==0){
 		   		DB::table('bkuser')->where('id',$request->id)->update(['state'=>1]);
 		   		return 1;
			}else{
 		   		DB::table('bkuser')->where('id',$request->id)->update(['state'=>0]);
 		   		return 0;
			}
    	}else{
    		if(DB::table('bkuser')->where('id',$request->id)->update($request->except(['id','confirm_password']))){
	    		return 1;
	    	}else{
	    		return 0;
	    	}
    	}
		
    }
}
